package com.neuedu.web.service;

import com.neuedu.web.common.PageInfo;
import com.neuedu.web.dao.InspectItemDao;
import com.neuedu.web.po.InspectitemPO;
import com.neuedu.web.utils.BeanConvertUtils;
import com.neuedu.web.utils.ParamUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.List;

/**
 * @author 金山
 * 项目：javaweb
 * site: https://blog.fulfill.com.cn
 * 描述  Dept 业务工呢
 * @data 2024年11月21日14:23:14
 */
public class InspectItemService {

    private InspectItemDao inspectItemDao = new InspectItemDao();


    public List list(HttpServletRequest request, HttpServletResponse response) throws IOException, NoSuchFieldException, InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException {


        String sql = " select * from inspectitem";
        List<InspectitemPO> list = inspectItemDao.selectAll(sql);

        return list;

    }


    public PageInfo<InspectitemPO> page(HttpServletRequest request, HttpServletResponse response) throws NoSuchFieldException, InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException {


        String sql = " select * from inspectitem";


        PageInfo pageInfo = ParamUtils.getPageInfo(request);
        List<InspectitemPO> dataList = inspectItemDao.selectPage(pageInfo.getCurrent(), pageInfo.getSize(), sql);
        int total = inspectItemDao.total(sql);

        pageInfo.setTotal(total);

        // 数据
        pageInfo.setData(dataList);

        return pageInfo;

    }

    /**
     * 保存
     * @param request
     * @param response
     * @return
     */
    public boolean save(HttpServletRequest request, HttpServletResponse response) {
        InspectitemPO data = ParamUtils.getParam(InspectitemPO.class, request);
        if(data.getInspectId() != null){
            return  inspectItemDao.update(data) >0;

        }else{
            return  inspectItemDao.insert(data) >0;

        }
    }

    public boolean delete(HttpServletRequest request, HttpServletResponse response) {
        String id = request.getParameter("id");

        return inspectItemDao.delete(Long.parseLong(id)) >0;
    }


    public InspectitemPO getById(HttpServletRequest request, HttpServletResponse response) throws NoSuchFieldException, InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException {
        String id = request.getParameter("id");

        return BeanConvertUtils.conver(InspectitemPO.class,inspectItemDao.selectById(Long.parseLong(id))) ;
    }
}
